package com.springboot.project.common.controller;

import com.springboot.project.common.entity.Dict;
import com.springboot.project.common.dao.DictDao;
import com.springboot.project.common.service.impl.DictServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.springboot.config.annotion.BusinessLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import com.springboot.config.tool.*;

import java.util.List;
import java.util.Map;

/**
 * 系统字典
 * 
 * @author lby
 * @email 2571664742@qq.com
 * @date 2018-07-01
 */
 
@Controller
@RequestMapping("/dict")
public class DictController {
	@Autowired
	private DictServiceImpl dictService;

    private static String PREFIX = "system/dict/";
    /**
     * 跳转到查看 系统字典列表的页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "dict";
    }

    /**
     * 加载表格
     */
    @RequestMapping(value = "/list")
    @ResponseBody
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<Dict> dictList = dictService.list(query);
		int total = dictService.count(query);
		PageUtils pageUtils = new PageUtils(dictList, total);
		return pageUtils;
	}

    /**
     * 跳转到添加页面
     */
    @RequestMapping("/add")
    @RequiresPermissions("/dict/add")
    public String addView() {
        return PREFIX + "dict_add";
    }


    /**
     * 跳转到修改页面
     */
    @RequestMapping("/edit/{id}")
    @RequiresPermissions("/dict/edit")
    public String userEdit(@PathVariable Integer id, Model model) {

		Dict dict = dictService.selectById(id);
        model.addAttribute("dict",dict);
        return PREFIX + "dict_edit";
    }

    /**
     * 删除 系统字典指定数据
     */
    @RequestMapping("/remove/{id}")
    @BusinessLog(value = "删除字典")
    @RequiresPermissions("/dict/del")
    @ResponseBody
    public Tip remove(@PathVariable Integer id) {
		dictService.deleteById(id);
        return new Tip();
    }

    /**
     * 添加系统字典
     */
    @RequestMapping("/addDict")
    @BusinessLog(value = "添加字典")
    @RequiresPermissions("/dict/add")
    @ResponseBody
    public Tip addDict(Dict dict) {
		dictService.insert(dict);
        return new Tip();
    }

    /**
     * 修改系统字典
     */
    @RequestMapping("/editDict")
    @BusinessLog(value = "修改字典")
    @RequiresPermissions("/dict/edit")
    @ResponseBody
    public Tip editDict(Dict dict) {
		dictService.updateById(dict);
        return new Tip();
    }
}
